package md.mru.frontend.controllers;

import md.mru.core.dtos.FolderDto;
import md.mru.core.dtos.UserDto;
import md.mru.core.dtos.UserGroupDto;
import md.mru.core.service.AdministrationService;
import md.mru.core.service.FolderService;
import org.apache.log4j.Logger;
import org.springframework.security.crypto.bcrypt.BCrypt;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 31/05/2014.
 */
@ManagedBean
@ViewScoped
public class UserAdministrationBean extends BackingBean implements Serializable {

    private static final long serialVersionUID = 2020341478311620509L;

    Logger logger = Logger.getLogger(UserAdministrationBean.class);

    @ManagedProperty(value = "#{authenticationBean}")
    private AuthenticationBean authenticationBean;

    @ManagedProperty(value = "#{administrationService}")
    private AdministrationService administrationService;

    @ManagedProperty(value = "#{folderService}")
    private FolderService folderService;

    private List<UserGroupDto> groups;
    private List<UserDto> users;
    private UserDto selectedUser;
    private List<FolderDto> unAssociatedFolders;

    public void initializeUsersContent() {
        users = administrationService.getAllUsers(authenticationBean.getLoginUser().getUser().getId());
        this.unAssociatedFolders = folderService.getUnAssociatedFolders();
        getObjectMappingConverter().mapObjectList(unAssociatedFolders);
        this.groups = administrationService.getGroups();
        getObjectMappingConverter().mapObjectList(groups);
    }

    public void resetCreateUserDialog() {
        this.selectedUser = new UserDto();
    }

    public void saveUserProfile() {
        selectedUser.setPassword(BCrypt.hashpw(selectedUser.getPassword(), BCrypt.gensalt()));
        this.administrationService.saveUser(selectedUser);
    }

    public void saveUser() {
        if (this.selectedUser.getPassword() == null) {
            this.selectedUser.setPassword(BCrypt.hashpw("password", BCrypt.gensalt()));
        }
        long userId = administrationService.saveUser(selectedUser);
        if (selectedUser.getId() == 0) {
            selectedUser.setId(userId);
            users.add(selectedUser);

            //TODO - send notification mail
            logger.debug("Inform e-mail was sent.");
        }
    }

    public List<FolderDto> complete(String suggestion) {
        List<FolderDto> suggestions = new ArrayList<FolderDto>();

        for (FolderDto f : unAssociatedFolders) {
            if (f.getFirstName().toLowerCase().startsWith(suggestion.toLowerCase())
                    || f.getLastName().toLowerCase().startsWith(suggestion.toLowerCase())) {
                suggestions.add(f);
            }
        }
        return suggestions;
    }

    /*public void onFolderSelect() {
        String generatedValue = this.selectedUser.getFolder().getFirstName().toLowerCase()
                + "." + this.selectedUser.getFolder().getLastName().toLowerCase();
        this.selectedUser.setUsername(generatedValue);
        this.selectedUser.setGeneratedPassword(generatedValue);
    }*/

    /*public void resetPassword() {
        this.selectedUser.setPassword(null);
        this.selectedUser.setGeneratedPassword(this.selectedUser.getUsername());
        this.selectedUser.setPasswordResetted(true);
    }*/

    public void editProfile() {
        this.selectedUser = new UserDto(authenticationBean.getLoginUser().getUser());
    }

    public void deleteUser(UserDto user) {
        administrationService.deleteUser(user.getId());
        this.users.remove(user);
    }

    public boolean editUserDisabled() {
        return this.selectedUser.getId() != 0;
    }

    public List<UserDto> getUsers() {
        return users;
    }

    public UserDto getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(UserDto selectedUser) {
        this.selectedUser = selectedUser;
    }

    public void setAdministrationService(AdministrationService administrationService) {
        this.administrationService = administrationService;
    }

    public void setAuthenticationBean(AuthenticationBean authenticationBean) {
        this.authenticationBean = authenticationBean;
    }

    public List<UserGroupDto> getGroups() {
        return groups;
    }

    public void setFolderService(FolderService folderService) {
        this.folderService = folderService;
    }
}
